package main

import (
	"errors"
	"fmt"
	"time"
)

func doSomething(done chan<- int) {
	cnt := 0
	for i := 0; i < 1000000000; i++ {
		cnt++
	}
	done <- cnt
}

func requestWithTimeout(timeout time.Duration) (int, error) {
	c := make(chan int)
	go doSomething(c)

	select {
	case data := <-c:
		return data, nil
	case <-time.After(timeout):
		return 0, errors.New("timeout")
	}
}

func main() {
	data, err := requestWithTimeout(time.Nanosecond)
	fmt.Println(data, err)
}
